package com.hctforgreen.greenservice.ui.widget;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.hctforgreen.greenservice.MessageFirstActivityV2;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.MessageTopThirdEntity;
import com.hctforgreen.greenservice.ui.adapter.MessageTopThirdLstAdapter;

public class MessageTopThirdPopupView extends PopupWindow {

	private View mConvertView;
	private Activity mActivity;

	private MessageTopThirdEntity mCurrentFirstEntity;
	private ArrayList<MessageTopThirdEntity> mEntities = new ArrayList<MessageTopThirdEntity>();

	private MessageFirstActivityV2 mSelf;

	public MessageTopThirdPopupView(Activity activity,
			MessageTopThirdEntity currentFirstEntity,
			MessageFirstActivityV2 self) {
		super(activity);
		mActivity = activity;

		mCurrentFirstEntity = currentFirstEntity;
		mSelf = self;
		initDataLst();
		initWindow();
	}

	/**
	 * 
	 * @param type 0:三个月前，1:一年前
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	private String getDateFrom(int type) {
		java.util.Date date = new java.util.Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		if(type == 0){
			long lTime = (date.getTime() / 1000) - 60 * 60 * 24 * 90;
			date.setTime(lTime * 1000);
			String sDate = format.format(date);
			return sDate;
		}else if(type == 1){
			long lTime = (date.getTime() / 1000) - 60 * 60 * 24 * 365;
			date.setTime(lTime * 1000);
			String sDate = format.format(date);
			return sDate;
		}else{
			return "";
		}
		
	}

	private void initDataLst() {
		MessageTopThirdEntity entity = new MessageTopThirdEntity();
		entity.name = mActivity.getString(R.string.message_top_third_01_hint);
		entity.dateFrom = getDateFrom(0);
		mEntities.add(entity);

		entity = new MessageTopThirdEntity();
		entity.name = mActivity.getString(R.string.message_top_third_02_hint);
		entity.dateFrom = getDateFrom(1);
		mEntities.add(entity);

		entity = new MessageTopThirdEntity();
		entity.name = mActivity.getString(R.string.message_top_third_03_hint);
		entity.dateFrom = getDateFrom(2);
		mEntities.add(entity);
	}

	private void initWindow() {
		mConvertView = getLayout();

		this.setContentView(mConvertView);
		this.setBackgroundDrawable(new BitmapDrawable());
		this.setOutsideTouchable(true);
		this.setFocusable(true);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);

		loadDataLst();
	}

	private View getLayout() {
		View layout = mActivity.getLayoutInflater().inflate(
				R.layout.popup_message_top_list, null);
		return layout;
	}

	private void loadDataLst() {
		final ListView firstLv = (ListView) mConvertView
				.findViewById(R.id.lst_default_list);

		renderDataLst(firstLv, mEntities);

	}

	private void renderDataLst(ListView firstLv,
			List<MessageTopThirdEntity> entities) {

		if (entities == null || entities.size() == 0) {
			return;
		}

		new MessageTopThirdLstAdapter(firstLv, mActivity, entities,
				mCurrentFirstEntity, this);
	}

	public void updateWindow(MessageTopThirdEntity currentThirdEntity) {
		mSelf.updateThirdEntity(currentThirdEntity);
		mSelf.updateThirdBtn(currentThirdEntity);
	}
}
